|
1
|
|
|
import chai, { expect } from 'chai' |
|
2
|
|
|
import chaiHttp from 'chai-http' |
|
3
|
|
|
import xmldoc from 'xmldoc' |
|
4
|
|
|
import fs from 'fs' |
|
5
|
|
|
import server from './server' |
|
6
|
|
|
|
|
7
|
|
|
chai.use(chaiHttp) |
|
8
|
|
|
|
|
9
|
|
|
// describe('Runtime arguments', () => { |
|
10
|
|
|
// it('should clean the output directory when called with --clean', (done) => { |
|
11
|
|
|
// process.argv = ['--clean'] |
|
12
|
|
|
// chai.request(server) |
|
13
|
|
|
// }) |
|
14
|
|
|
// }) |
|
15
|
|
|
const toAddress = '[email protected]' |
|
16
|
|
|
const fromEmail = '[email protected]' |
|
17
|
|
|
|
|
18
|
|
|
describe('/POST SendEmail', () => { |
|
19
|
|
|
const ccAddress = '[email protected]' |
|
20
|
|
|
const bccAddress = '[email protected]' |
|
21
|
|
|
const replyToAddress = '[email protected]' |
|
22
|
|
|
const htmlEmail = '<p>HTML Email</p>' |
|
23
|
|
|
const textEmail = 'Text Email' |
|
24
|
|
|
const emailSubject = 'Email Subject 😊' |
|
25
|
|
|
it('should succeed if email has all params', (done) => { |
|
26
|
|
|
chai.request(server) |
|
27
|
|
|
.post('/') |
|
28
|
|
|
.send({ |
|
29
|
|
|
Action: 'SendEmail', |
|
30
|
|
|
'Destination.ToAddresses.member.1': toAddress, |
|
31
|
|
|
'Message.Body.Html.Data': htmlEmail, |
|
32
|
|
|
'Message.Body.Text.Data': textEmail, |
|
33
|
|
|
'Message.Subject.Data': emailSubject, |
|
34
|
|
|
'Destination.CcAddresses.member.1': ccAddress, |
|
35
|
|
|
'Destination.BccAddresses.member.1': bccAddress, |
|
36
|
|
|
'ReplyToAddresses.member.1': replyToAddress, |
|
37
|
|
|
Source: fromEmail, |
|
38
|
|
|
}) |
|
39
|
|
|
.end((err, res) => { |
|
40
|
|
|
expect(res).to.have.status(200) |
|
41
|
|
|
const response = new xmldoc.XmlDocument(res.text) |
|
42
|
|
|
const path = response.valueWithPath('SendEmailResult.MessageId') |
|
43
|
|
|
expect(fs.readFileSync(path, 'utf8')).to.eq(htmlEmail) |
|
44
|
|
|
expect(fs.readFileSync(path.replace('body.html', 'body.txt'), 'utf8')).to.eq(textEmail) |
|
45
|
|
|
expect(fs.readFileSync(path.replace('body.html', 'headers.txt'), 'utf8')).to.eq(`Subject: ${emailSubject}\nTo Address: ${toAddress}\nCc Address: ${ccAddress}\nBcc Address: ${bccAddress}\nReply To Address: ${replyToAddress}\nSource: ${fromEmail}`) |
|
46
|
|
|
done() |
|
47
|
|
|
}) |
|
48
|
|
|
}) |
|
49
|
|
|
it('should succeed if only HTML body is missing', (done) => { |
|
50
|
|
|
chai.request(server) |
|
51
|
|
|
.post('/') |
|
52
|
|
|
.send({ |
|
53
|
|
|
Action: 'SendEmail', |
|
54
|
|
|
'Destination.ToAddresses.member.1': toAddress, |
|
55
|
|
|
'Message.Body.Text.Data': textEmail, |
|
56
|
|
|
'Message.Subject.Data': emailSubject, |
|
57
|
|
|
'Destination.CcAddresses.member.1': ccAddress, |
|
58
|
|
|
'Destination.BccAddresses.member.1': bccAddress, |
|
59
|
|
|
'ReplyToAddresses.member.1': replyToAddress, |
|
60
|
|
|
Source: fromEmail, |
|
61
|
|
|
}) |
|
62
|
|
|
.end((err, res) => { |
|
63
|
|
|
expect(res).to.have.status(200) |
|
64
|
|
|
done() |
|
65
|
|
|
}) |
|
66
|
|
|
}) |
|
67
|
|
|
it('should succeed if only Text body is missing', (done) => { |
|
68
|
|
|
chai.request(server) |
|
69
|
|
|
.post('/') |
|
70
|
|
|
.send({ |
|
71
|
|
|
Action: 'SendEmail', |
|
72
|
|
|
'Destination.ToAddresses.member.1': toAddress, |
|
73
|
|
|
'Message.Body.Html.Data': htmlEmail, |
|
74
|
|
|
'Message.Subject.Data': emailSubject, |
|
75
|
|
|
'Destination.CcAddresses.member.1': ccAddress, |
|
76
|
|
|
'Destination.BccAddresses.member.1': bccAddress, |
|
77
|
|
|
'ReplyToAddresses.member.1': replyToAddress, |
|
78
|
|
|
Source: fromEmail, |
|
79
|
|
|
}) |
|
80
|
|
|
.end((err, res) => { |
|
81
|
|
|
expect(res).to.have.status(200) |
|
82
|
|
|
done() |
|
83
|
|
|
}) |
|
84
|
|
|
}) |
|
85
|
|
|
it('should fail if one param is not sent', (done) => { |
|
86
|
|
|
chai.request(server) |
|
87
|
|
|
.post('/') |
|
88
|
|
|
.send({ |
|
89
|
|
|
Action: 'SendEmail', |
|
90
|
|
|
'Message.Body.Html.Data': htmlEmail, |
|
91
|
|
|
'Message.Body.Text.Data': textEmail, |
|
92
|
|
|
'Message.Subject.Data': emailSubject, |
|
93
|
|
|
Source: fromEmail, |
|
94
|
|
|
}) |
|
95
|
|
|
.end((err, res) => { |
|
96
|
|
|
expect(res).to.have.status(500) |
|
97
|
|
|
const response = new xmldoc.XmlDocument(res.text) |
|
98
|
|
|
expect(response.valueWithPath('Code')).to.eq('MessageRejected') |
|
99
|
|
|
expect(response.valueWithPath('Message')).to.eq('One or more required fields was not sent') |
|
100
|
|
|
done() |
|
101
|
|
|
}) |
|
102
|
|
|
}) |
|
103
|
|
|
}) |
|
104
|
|
|
|
|
105
|
|
|
describe('/POST Unsupported action', () => { |
|
106
|
|
|
it('should fail if the action is not unsupported', (done) => { |
|
107
|
|
|
chai.request(server) |
|
108
|
|
|
.post('/') |
|
109
|
|
|
.send({ Action: 'SomeRandomAction' }) |
|
110
|
|
|
.end((err, res) => { |
|
111
|
|
|
expect(res).to.have.status(500) |
|
112
|
|
|
const response = new xmldoc.XmlDocument(res.text) |
|
113
|
|
|
expect(response.valueWithPath('Code')).to.eq('MessageRejected') |
|
114
|
|
|
expect(response.valueWithPath('Message')).to.eq('Unsupported action SomeRandomAction') |
|
115
|
|
|
done() |
|
116
|
|
|
}) |
|
117
|
|
|
}) |
|
118
|
|
|
}) |
|
119
|
|
|
|
|
120
|
|
|
describe('/POST SendRawEmail', () => { |
|
121
|
|
|
it('should write the decoded message to disk', (done) => { |
|
122
|
|
|
chai.request(server) |
|
123
|
|
|
.post('/') |
|
124
|
|
|
.send({ |
|
125
|
|
|
Action: 'SendRawEmail', |
|
126
|
|
|
'Destinations.member.1': toAddress, |
|
127
|
|
|
'RawMessage.Data': Buffer.from('Some raw email data').toString('base64'), |
|
|
|
|
|
|
128
|
|
|
Source: fromEmail, |
|
129
|
|
|
}) |
|
130
|
|
|
.end((err, res) => { |
|
131
|
|
|
expect(res).to.have.status(200) |
|
132
|
|
|
const response = new xmldoc.XmlDocument(res.text) |
|
133
|
|
|
const path = response.valueWithPath('SendEmailResult.MessageId') |
|
134
|
|
|
expect(fs.readFileSync(path, 'utf8')).to.eq('Some raw email data') |
|
135
|
|
|
done() |
|
136
|
|
|
}) |
|
137
|
|
|
}) |
|
138
|
|
|
|
|
139
|
|
|
it('should fail if the raw message data is not sent', (done) => { |
|
140
|
|
|
chai.request(server) |
|
141
|
|
|
.post('/') |
|
142
|
|
|
.send({ |
|
143
|
|
|
Action: 'SendRawEmail', |
|
144
|
|
|
}) |
|
145
|
|
|
.end((err, res) => { |
|
146
|
|
|
expect(res).to.have.status(500) |
|
147
|
|
|
done() |
|
148
|
|
|
}) |
|
149
|
|
|
}) |
|
150
|
|
|
}) |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.